﻿import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import FilUI

TextArea {
    id: control

    property bool disabled                      : false
    property bool isCtrlEnterForNewline         : false
    property color normalColor                  : Qt.rgba(27/255, 27/255, 27/255, 1)
    property color disableColor                 : Qt.rgba(160/255, 160/255, 160/255, 1)
    property color placeholderNormalColor       : Qt.rgba(96/255, 96/255, 96/255, 1)
    property color placeholderFocusColor        : Qt.rgba(141/255, 141/255, 141/255, 1)
    property color placeholderDisableColor      : Qt.rgba(160/255, 160/255, 160/255, 1)

    signal commit(string text)

    enabled                                     : !disabled
    color                                       : {
        if (!enabled) {
            return disableColor
        }
        return normalColor
    }
    font                                        : FilTextStyle.Body
    wrapMode                                    : Text.WordWrap
    padding                                     : 8
    leftPadding                                 : padding + 4
    renderType                                  : Text.QtRendering
    selectedTextColor                           : color
    selectionColor                              : Qt.rgba(76/255, 160/255, 224/255, 0.5)
    placeholderTextColor                        : {
        if (!enabled) {
            return placeholderDisableColor
        }
        if (focus) {
            return placeholderFocusColor
        }
        return placeholderNormalColor
    }
    selectByMouse                               : true
    width                                       : 240
    background                                  : FilTextBoxBackground {
        inputItem                               : control
    }
    Keys.onEnterPressed                         : (event) => d.handleCommit(event)
    Keys.onReturnPressed                        : (event) => d.handleCommit(event)

    QtObject {
        id: d
        function handleCommit(event) {
            if (isCtrlEnterForNewline) {
                if (event.modifiers & Qt.ControlModifier) {
                    insert(control.cursorPosition, "\n")
                    return
                }
                control.commit(control.text)
            } else {
                if (event.modifiers & Qt.ControlModifier) {
                    control.commit(control.text)
                    return
                }
                insert(control.cursorPosition, "\n")
            }
        }
    }

    MouseArea {
        anchors.fill                            : parent
        cursorShape                             : Qt.IBeamCursor
        acceptedButtons                         : Qt.RightButton
        onClicked                               : {
            if (control.echoMode === TextInput.Password) {
                return
            }
            if (control.readOnly && control.text === "") {
                return
            }
            menu_loader.popup()
        }
    }

    Loader {
        id: menu_loader
        Component.onDestruction                 : sourceComponent = undefined
        function popup() {
            sourceComponent = menu
        }
    }

    Component{
        id: menu
        FilTextBoxMenu {
            inputItem                           : control
            Component.onCompleted               : {
                popup()
            }
            onClosed                            : {
                menu_loader.sourceComponent = undefined
            }
        }
    }
}
